﻿/**  版本信息模板在安装目录下，可自行修改。
* s_user.cs
*
* 功 能： N/A
* 类 名： s_user
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2018/1/2 22:18:53   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using ZGEDrySaltery.IDAL;
using Maticsoft.DBUtility;//Please add references
namespace ZGEDrySaltery.MySQLDAL
{
	/// <summary>
	/// 数据访问类:s_user
	/// </summary>
	public partial class s_user:Is_user
	{
		public s_user()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperMySQL.GetMaxID("USER_ID", "s_user"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int USER_ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from s_user");
			strSql.Append(" where USER_ID=@USER_ID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@USER_ID", MySqlDbType.Int32)
			};
			parameters[0].Value = USER_ID;

			return DbHelperMySQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(ZGEDrySaltery.Model.s_user model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into s_user(");
			strSql.Append("ORG_ID,USER_NAME,REAL_NAME,SEX,ID_CARD,USER_PSD,OPEN_ID,TEL,EMAIL,IMAGE_PATH,LAST_ONLINE_TIME,LOGIN_TIMES,ENABLE_FLAG,CREATE_USER_ID,CREATE_TIME,UPDATE_USER_ID,UPDATE_TIME,REMARK,TENANT_ID)");
			strSql.Append(" values (");
			strSql.Append("@ORG_ID,@USER_NAME,@REAL_NAME,@SEX,@ID_CARD,@USER_PSD,@OPEN_ID,@TEL,@EMAIL,@IMAGE_PATH,@LAST_ONLINE_TIME,@LOGIN_TIMES,@ENABLE_FLAG,@CREATE_USER_ID,@CREATE_TIME,@UPDATE_USER_ID,@UPDATE_TIME,@REMARK,@TENANT_ID)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@ORG_ID", MySqlDbType.Int32,11),
					new MySqlParameter("@USER_NAME", MySqlDbType.VarChar,32),
					new MySqlParameter("@REAL_NAME", MySqlDbType.VarChar,32),
					new MySqlParameter("@SEX", MySqlDbType.VarChar,1),
					new MySqlParameter("@ID_CARD", MySqlDbType.VarChar,18),
					new MySqlParameter("@USER_PSD", MySqlDbType.VarChar,32),
					new MySqlParameter("@OPEN_ID", MySqlDbType.VarChar,128),
					new MySqlParameter("@TEL", MySqlDbType.VarChar,32),
					new MySqlParameter("@EMAIL", MySqlDbType.VarChar,32),
					new MySqlParameter("@IMAGE_PATH", MySqlDbType.VarChar,512),
					new MySqlParameter("@LAST_ONLINE_TIME", MySqlDbType.Timestamp),
					new MySqlParameter("@LOGIN_TIMES", MySqlDbType.Int32,11),
					new MySqlParameter("@ENABLE_FLAG", MySqlDbType.VarChar,1),
					new MySqlParameter("@CREATE_USER_ID", MySqlDbType.VarChar,32),
					new MySqlParameter("@CREATE_TIME", MySqlDbType.Timestamp),
					new MySqlParameter("@UPDATE_USER_ID", MySqlDbType.VarChar,32),
					new MySqlParameter("@UPDATE_TIME", MySqlDbType.Timestamp),
					new MySqlParameter("@REMARK", MySqlDbType.VarChar,256),
					new MySqlParameter("@TENANT_ID", MySqlDbType.Int32,11)};
			parameters[0].Value = model.ORG_ID;
			parameters[1].Value = model.USER_NAME;
			parameters[2].Value = model.REAL_NAME;
			parameters[3].Value = model.SEX;
			parameters[4].Value = model.ID_CARD;
			parameters[5].Value = model.USER_PSD;
			parameters[6].Value = model.OPEN_ID;
			parameters[7].Value = model.TEL;
			parameters[8].Value = model.EMAIL;
			parameters[9].Value = model.IMAGE_PATH;
			parameters[10].Value = model.LAST_ONLINE_TIME;
			parameters[11].Value = model.LOGIN_TIMES;
			parameters[12].Value = model.ENABLE_FLAG;
			parameters[13].Value = model.CREATE_USER_ID;
			parameters[14].Value = model.CREATE_TIME;
			parameters[15].Value = model.UPDATE_USER_ID;
			parameters[16].Value = model.UPDATE_TIME;
			parameters[17].Value = model.REMARK;
			parameters[18].Value = model.TENANT_ID;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(ZGEDrySaltery.Model.s_user model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update s_user set ");
			strSql.Append("ORG_ID=@ORG_ID,");
			strSql.Append("USER_NAME=@USER_NAME,");
			strSql.Append("REAL_NAME=@REAL_NAME,");
			strSql.Append("SEX=@SEX,");
			strSql.Append("ID_CARD=@ID_CARD,");
			strSql.Append("USER_PSD=@USER_PSD,");
			strSql.Append("OPEN_ID=@OPEN_ID,");
			strSql.Append("TEL=@TEL,");
			strSql.Append("EMAIL=@EMAIL,");
			strSql.Append("IMAGE_PATH=@IMAGE_PATH,");
			strSql.Append("LOGIN_TIMES=@LOGIN_TIMES,");
			strSql.Append("ENABLE_FLAG=@ENABLE_FLAG,");
			strSql.Append("CREATE_USER_ID=@CREATE_USER_ID,");
			strSql.Append("UPDATE_USER_ID=@UPDATE_USER_ID,");
			strSql.Append("REMARK=@REMARK,");
			strSql.Append("TENANT_ID=@TENANT_ID");
			strSql.Append(" where USER_ID=@USER_ID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@ORG_ID", MySqlDbType.Int32,11),
					new MySqlParameter("@USER_NAME", MySqlDbType.VarChar,32),
					new MySqlParameter("@REAL_NAME", MySqlDbType.VarChar,32),
					new MySqlParameter("@SEX", MySqlDbType.VarChar,1),
					new MySqlParameter("@ID_CARD", MySqlDbType.VarChar,18),
					new MySqlParameter("@USER_PSD", MySqlDbType.VarChar,32),
					new MySqlParameter("@OPEN_ID", MySqlDbType.VarChar,128),
					new MySqlParameter("@TEL", MySqlDbType.VarChar,32),
					new MySqlParameter("@EMAIL", MySqlDbType.VarChar,32),
					new MySqlParameter("@IMAGE_PATH", MySqlDbType.VarChar,512),
					new MySqlParameter("@LOGIN_TIMES", MySqlDbType.Int32,11),
					new MySqlParameter("@ENABLE_FLAG", MySqlDbType.VarChar,1),
					new MySqlParameter("@CREATE_USER_ID", MySqlDbType.VarChar,32),
					new MySqlParameter("@UPDATE_USER_ID", MySqlDbType.VarChar,32),
					new MySqlParameter("@REMARK", MySqlDbType.VarChar,256),
					new MySqlParameter("@TENANT_ID", MySqlDbType.Int32,11),
					new MySqlParameter("@USER_ID", MySqlDbType.Int32,11)};
			parameters[0].Value = model.ORG_ID;
			parameters[1].Value = model.USER_NAME;
			parameters[2].Value = model.REAL_NAME;
			parameters[3].Value = model.SEX;
			parameters[4].Value = model.ID_CARD;
			parameters[5].Value = model.USER_PSD;
			parameters[6].Value = model.OPEN_ID;
			parameters[7].Value = model.TEL;
			parameters[8].Value = model.EMAIL;
			parameters[9].Value = model.IMAGE_PATH;
			parameters[10].Value = model.LOGIN_TIMES;
			parameters[11].Value = model.ENABLE_FLAG;
			parameters[12].Value = model.CREATE_USER_ID;
			parameters[13].Value = model.UPDATE_USER_ID;
			parameters[14].Value = model.REMARK;
			parameters[15].Value = model.TENANT_ID;
			parameters[16].Value = model.USER_ID;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int USER_ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from s_user ");
			strSql.Append(" where USER_ID=@USER_ID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@USER_ID", MySqlDbType.Int32)
			};
			parameters[0].Value = USER_ID;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string USER_IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from s_user ");
			strSql.Append(" where USER_ID in ("+USER_IDlist + ")  ");
			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ZGEDrySaltery.Model.s_user GetModel(int USER_ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select USER_ID,ORG_ID,USER_NAME,REAL_NAME,SEX,ID_CARD,USER_PSD,OPEN_ID,TEL,EMAIL,IMAGE_PATH,LAST_ONLINE_TIME,LOGIN_TIMES,ENABLE_FLAG,CREATE_USER_ID,CREATE_TIME,UPDATE_USER_ID,UPDATE_TIME,REMARK,TENANT_ID from s_user ");
			strSql.Append(" where USER_ID=@USER_ID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@USER_ID", MySqlDbType.Int32)
			};
			parameters[0].Value = USER_ID;

			ZGEDrySaltery.Model.s_user model=new ZGEDrySaltery.Model.s_user();
			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public ZGEDrySaltery.Model.s_user DataRowToModel(DataRow row)
		{
			ZGEDrySaltery.Model.s_user model=new ZGEDrySaltery.Model.s_user();
			if (row != null)
			{
				if(row["USER_ID"]!=null && row["USER_ID"].ToString()!="")
				{
					model.USER_ID=int.Parse(row["USER_ID"].ToString());
				}
				if(row["ORG_ID"]!=null && row["ORG_ID"].ToString()!="")
				{
					model.ORG_ID=int.Parse(row["ORG_ID"].ToString());
				}
				if(row["USER_NAME"]!=null)
				{
					model.USER_NAME=row["USER_NAME"].ToString();
				}
				if(row["REAL_NAME"]!=null)
				{
					model.REAL_NAME=row["REAL_NAME"].ToString();
				}
				if(row["SEX"]!=null)
				{
					model.SEX=row["SEX"].ToString();
				}
				if(row["ID_CARD"]!=null)
				{
					model.ID_CARD=row["ID_CARD"].ToString();
				}
				if(row["USER_PSD"]!=null)
				{
					model.USER_PSD=row["USER_PSD"].ToString();
				}
				if(row["OPEN_ID"]!=null)
				{
					model.OPEN_ID=row["OPEN_ID"].ToString();
				}
				if(row["TEL"]!=null)
				{
					model.TEL=row["TEL"].ToString();
				}
				if(row["EMAIL"]!=null)
				{
					model.EMAIL=row["EMAIL"].ToString();
				}
				if(row["IMAGE_PATH"]!=null)
				{
					model.IMAGE_PATH=row["IMAGE_PATH"].ToString();
				}
				if(row["LAST_ONLINE_TIME"]!=null && row["LAST_ONLINE_TIME"].ToString()!="")
				{
					model.LAST_ONLINE_TIME=DateTime.Parse(row["LAST_ONLINE_TIME"].ToString());
				}
				if(row["LOGIN_TIMES"]!=null && row["LOGIN_TIMES"].ToString()!="")
				{
					model.LOGIN_TIMES=int.Parse(row["LOGIN_TIMES"].ToString());
				}
				if(row["ENABLE_FLAG"]!=null)
				{
					model.ENABLE_FLAG=row["ENABLE_FLAG"].ToString();
				}
				if(row["CREATE_USER_ID"]!=null)
				{
					model.CREATE_USER_ID=row["CREATE_USER_ID"].ToString();
				}
				if(row["CREATE_TIME"]!=null && row["CREATE_TIME"].ToString()!="")
				{
					model.CREATE_TIME=DateTime.Parse(row["CREATE_TIME"].ToString());
				}
				if(row["UPDATE_USER_ID"]!=null)
				{
					model.UPDATE_USER_ID=row["UPDATE_USER_ID"].ToString();
				}
				if(row["UPDATE_TIME"]!=null && row["UPDATE_TIME"].ToString()!="")
				{
					model.UPDATE_TIME=DateTime.Parse(row["UPDATE_TIME"].ToString());
				}
				if(row["REMARK"]!=null)
				{
					model.REMARK=row["REMARK"].ToString();
				}
				if(row["TENANT_ID"]!=null && row["TENANT_ID"].ToString()!="")
				{
					model.TENANT_ID=int.Parse(row["TENANT_ID"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select USER_ID,ORG_ID,USER_NAME,REAL_NAME,SEX,ID_CARD,USER_PSD,OPEN_ID,TEL,EMAIL,IMAGE_PATH,LAST_ONLINE_TIME,LOGIN_TIMES,ENABLE_FLAG,CREATE_USER_ID,CREATE_TIME,UPDATE_USER_ID,UPDATE_TIME,REMARK,TENANT_ID ");
			strSql.Append(" FROM s_user ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM s_user ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.USER_ID desc");
			}
			strSql.Append(")AS Row, T.*  from s_user T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "s_user";
			parameters[1].Value = "USER_ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

